﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.Model;
using AspNetCs.IDAL;
using System.Data;
using System.Data.SqlClient;

namespace AspNetCs.SQLServerDAL
{
    public class PartSiteConfig_DAL:IPartSiteConfig
    {
        /// <summary>
        /// 添加分站的配置
        /// </summary>
        /// <returns></returns>
        public int AddSiteConfig(PartSiteConfigModel model)
        {
            int count = 0;
            db.ParameterArry[] paras = new db.ParameterArry[8];
            paras[0].dbType = SqlDbType.VarChar;
            paras[0].parName = "@SiteName";
            paras[0].objValue = model.SiteName;

            paras[1].dbType = SqlDbType.VarChar;
            paras[1].parName = "@LogoURL";
            paras[1].objValue = model.LogoURL;

            paras[2].dbType = SqlDbType.VarChar;
            paras[2].parName = "@MangerName";
            paras[2].objValue = model.MangerName;

            paras[3].dbType = SqlDbType.VarChar;
            paras[3].parName = "@ManagerEmail";
            paras[3].objValue = model.ManagerEmail;

            paras[4].dbType = SqlDbType.VarChar;
            paras[4].parName = "@SiteQQ";
            paras[4].objValue = model.SiteQQ;

            paras[5].dbType = SqlDbType.Text;
            paras[5].parName = "@Version";
            paras[5].objValue = model.Version;

            paras[6].dbType = SqlDbType.Int;
            paras[6].parName = "@Province";
            paras[6].objValue = model.Province;

            paras[7].dbType = SqlDbType.Int;
            paras[7].parName = "@City";
            paras[7].objValue = model.City;

            string cmdText = "insert into PartSiteConfig values (@SiteName,@LogoURL,@MangerName,@ManagerEmail,@SiteQQ,@Version,@Province,@City)";
            db dbclass = new db();
            count = dbclass.ExcSql(cmdText,paras);
            return count;
        }

        /// <summary>
        /// 修改分站的配置
        /// </summary>
        /// <returns></returns>
        public int UpdateSiteConfig(PartSiteConfigModel model)
        {
            int count = 0;
            db.ParameterArry[] paras = new db.ParameterArry[7];
            paras[0].dbType = SqlDbType.VarChar;
            paras[0].parName = "@SiteName";
            paras[0].objValue = model.SiteName;

            paras[1].dbType = SqlDbType.VarChar;
            paras[1].parName = "@LogoURL";
            paras[1].objValue = model.LogoURL;

            paras[2].dbType = SqlDbType.VarChar;
            paras[2].parName = "@MangerName";
            paras[2].objValue = model.MangerName;

            paras[3].dbType = SqlDbType.VarChar;
            paras[3].parName = "@ManagerEmail";
            paras[3].objValue = model.ManagerEmail;

            paras[4].dbType = SqlDbType.VarChar;
            paras[4].parName = "@SiteQQ";
            paras[4].objValue = model.SiteQQ;

            paras[5].dbType = SqlDbType.Text;
            paras[5].parName = "@Version";
            paras[5].objValue = model.Version;

            paras[6].dbType = SqlDbType.Int;
            paras[6].parName = "@Id";
            paras[6].objValue = model.Id;


            string cmdText = "update PartSiteConfig set SiteName=@SiteName,LogoURL=@LogoURL,MangerName=@MangerName,ManagerEmail=@ManagerEmail,SiteQQ=@SiteQQ,Version=@Version where Id=@Id";
            db dbclass = new db();
            count = dbclass.ExcSql(cmdText, paras);
            return count;
        }

        /// <summary>
        /// 是否存在该分站的配置
        /// </summary>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <returns></returns>
        public bool IsExistSiteConfig(int province, int city)
        {

            int count = 0;
            db.ParameterArry[] paras = new db.ParameterArry[2];
            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@Province";
            paras[0].objValue = province;

            paras[1].dbType = SqlDbType.Int;
            paras[1].parName = "@City";
            paras[1].objValue = city;



            string cmdText = "select count(id) from PartSiteConfig where Province=@Province and City=@City";
            db dbclass = new db();
            count = Convert.ToInt32(dbclass.GetOneFile(cmdText, paras).ToString());
            
            return count>0?true:false;
        }

        /// <summary>
        /// 是否存在该分站的配置
        /// </summary>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <returns></returns>
        public PartSiteConfigModel GetModel(int province,int city)
        {
            db.ParameterArry[] paras = new db.ParameterArry[2];
            paras[0].dbType = SqlDbType.Int;
            paras[0].parName = "@Province";
            paras[0].objValue = province;

            paras[1].dbType = SqlDbType.Int;
            paras[1].parName = "@City";
            paras[1].objValue = city;



            string cmdText = "select * from PartSiteConfig where Province=@Province and City=@City";
            db dbclass = new db();
            SqlDataReader reader = dbclass.GetSqlDataReader(cmdText,paras);
            PartSiteConfigModel model = new PartSiteConfigModel();
            model.Id = 0;
            model.SiteName = "";
            model.LogoURL = "";
            model.MangerName = "";
            model.ManagerEmail = "";
            model.SiteQQ = "";
            model.Version = "";
            model.Province = 0;
            model.City = 0;
            if(reader.Read())
            {
                model.Id = reader.GetInt32(0);
                model.SiteName = reader.GetString(1);
                model.LogoURL = reader.GetString(2);
                model.MangerName = reader.GetString(3);
                model.ManagerEmail = reader.GetString(4);
                model.SiteQQ = reader.GetString(5);
                model.Version = reader.GetString(6);
                model.Province = reader.GetInt32(7);
                model.City = reader.GetInt32(8);
            }

            return model;
        }
    }
}
